package com.shanshan.wheel.rpc.common.loadbalance;

import java.net.UnknownHostException;
import java.util.List;

/**
 * @Description 负载均衡抽象类，提供选择策略抽象方法
 * @ClassName AbstractLoadBalance
 * @Author 山山大魔王
 * @Date 2022/9/4 11:39
 * @Version 1.0
 */
public abstract class AbstractLoadBalance implements LoadBalance{
    @Override
    public String selectServiceAddress(List<String> serviceAddress, String rpcServiceName) {
        if(serviceAddress == null || serviceAddress.size() == 0){
            return null;
        }

        if(serviceAddress.size() == 1){
            return serviceAddress.get(0);
        }

        try {
            return doSelect(serviceAddress, rpcServiceName);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 选择策略
     * @param serviceAddress
     * @param rpcServiceName
     * @return
     * @throws UnknownHostException
     */
    protected abstract String doSelect(List<String> serviceAddress, String rpcServiceName) throws UnknownHostException;

}
